
import OSS  from 'ali-oss';
import { ossmessage,ossuploading } from "@/api/oss"

//获取配置的方法
// const res = ossmessgtr()
// console.log('456',res);
// const thae = this
const getListDat = async () => {
      // console.log(api);
      const listinfo = await ossmessage({
        'ossGroup':'CommodityMainImages'
      })
   
    // let info = await $http.get(api.ossmessgtr)
    return {
        accessKeyId:listinfo.data.accessKeyId,
        accessKeySecret:listinfo.data.accessKeySecret,
        stsToken:listinfo.data.secretToken,
    }
};
//获取配置条件
const clients = (data) => {
//  console.log('123',getListDat());
  return new OSS({
    region: "oss-cn-hangzhou", //这个根据自己项目去填写(地区)
    accessKeyId:data.accessKeyId,
    accessKeySecret:data.accessKeySecret,
    stsToken:data.stsToken,
    bucket:"xn-panda-test", //这个根据自己项目去填写
  });
};
//获取uuid随机数作为文件名称
const getFileNameUUID = () => {

  function rx() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return `${+new Date()}_${rx()}${rx()}`;
};
//创建oss上传方法
const that = this
async function ossUploa(file,module='image'){
  //调用接口，获取配置
    let configInfsss = await getListDat();

    let fileType = file.type.split("/")[1]

    //拼接文件名称（拼接你需要保存的目录路径,'avatar/'这是我的目录路径）
    const listdata = await ossuploading({
      'fileName':'.jpg','ossGroup':'CommodityMainImages'
    })
  console.log('741',listdata);
    // let date = new Date();
    let fileNameUrle = `${listdata.data.path}`;

    let res = await clients(configInfsss).put(fileNameUrle,file);
    return res.url;
  
  }


export default ossUploa;

